﻿//----------------------------------------------------------------
// <copyright file="CodeGen.cs" >
//    Copyright (c) Wenzy , All rights reserved.
//    author:温正宇 E-Mail:wenzy@tom.com　MyBlog:wenzy.cnblogs.com
//    All rights reserved.
// </copyright>
//----------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Text;
using MemberShipDAL;
using MemberShipDAL.DataObject;
using System.Data;

namespace WMFConfig.Bussiness
{
    /// <summary>
    /// 代码生成
    /// </summary>
    internal class CodeGen
    {
        /// <summary>
        /// 生成Resource代码
        /// 这个方法写的快吐鲜血了，花了2个小时。在此纪念一下，温正宇：2006年9月7日
        /// 2006年9月17日，对这个方法进行改造
        /// </summary>
        /// <returns></returns>
        internal static string ResourceCode()
        {
            StringBuilder genCode = new StringBuilder();

            #region 版权信息
            genCode.Append("//----------------------------------------------------------------\n");
            genCode.Append("// 系统资源的枚举类\n");
            genCode.Append("// <copyright file=\"Privilege.cs\" company=\"Wenzy\">\n");
            genCode.Append("//    Copyright (c) Wenzy , All rights reserved.\n");
            genCode.Append("//    author:温正宇 E-Mail:wenzy@tom.com\n");
            genCode.Append("//    代码生成日期： " + DateTime.Now.ToString() + "\n");
            genCode.Append("// </copyright>\n");
            genCode.Append("//----------------------------------------------------------------\n\n");
            #endregion


            #region  生成操作信息
            genCode.Append("namespace Privilege\n");
            genCode.Append("{\n");
            //获得资源
            MemberShipDAL.DataObject.DataResourcesTableAdapters.wmf_ResourcesTableAdapter resDa = new MemberShipDAL.DataObject.DataResourcesTableAdapters.wmf_ResourcesTableAdapter();
            MemberShipDAL.DataObject.DataResources.wmf_ResourcesDataTable resDt = new DataResources.wmf_ResourcesDataTable();
            resDa.Fill(resDt);

            //获得资源组
            MemberShipDAL.DataObject.DataResourcesTableAdapters.wmf_ResourcesGroupTableAdapter resGrpDa = new MemberShipDAL.DataObject.DataResourcesTableAdapters.wmf_ResourcesGroupTableAdapter();
            MemberShipDAL.DataObject.DataResources.wmf_ResourcesGroupDataTable resGrpDt = new DataResources.wmf_ResourcesGroupDataTable();
            resGrpDa.Fill(resGrpDt);

            //获得权限
            MemberShipDAL.DataObject.DataResourcesTableAdapters.wmf_PrivilegeTableAdapter privilegeDa = new MemberShipDAL.DataObject.DataResourcesTableAdapters.wmf_PrivilegeTableAdapter();
            MemberShipDAL.DataObject.DataResources.wmf_PrivilegeDataTable privilegeDt = new DataResources.wmf_PrivilegeDataTable();
            privilegeDa.Fill(privilegeDt);

            //获得操作
            MemberShipDAL.DataObject.DataResourcesTableAdapters.wmf_OperationTableAdapter optDa = new MemberShipDAL.DataObject.DataResourcesTableAdapters.wmf_OperationTableAdapter();
            MemberShipDAL.DataObject.DataResources.wmf_OperationDataTable optDt = new DataResources.wmf_OperationDataTable();
            optDa.Fill(optDt);

            genCode.Append(GetPrivilegeString("", new StringBuilder(), resDt, resGrpDt, privilegeDt, optDt, ""));
            #endregion

            genCode.Append("}\n");
            return genCode.ToString();
        }

        /// <summary>
        /// 递归生成字符串
        /// </summary>
        /// <param name="groupParentId"></param>
        /// <param name="genCode"></param>
        /// <param name="resDt"></param>
        /// <param name="resGrpDt"></param>
        /// <param name="tab"></param>
        /// <returns></returns>
        private static string GetPrivilegeString(string groupParentId, StringBuilder genCode, DataResources.wmf_ResourcesDataTable resDt, DataResources.wmf_ResourcesGroupDataTable resGrpDt, DataResources.wmf_PrivilegeDataTable privilegeDt, DataResources.wmf_OperationDataTable optDt, string tab)
        {
            StringBuilder retValue = new StringBuilder();
            //加缩进用的
            tab = tab + "\t";

            DataView resGrpDv = new DataView(resGrpDt);

            if (groupParentId != "")
            {
                resGrpDv.RowFilter = "[ParentId] ='" + groupParentId + "'";
            }
            else
            {
                resGrpDv.RowFilter = "[ParentId] is null";
            }


            //生成权限组结构
            foreach (DataRowView gRow in resGrpDv)
            {

                if (genCode.ToString() == string.Empty)
                {

                    #region 注释
                    retValue.Append(tab).Append("/// <summary>\n");
                    try
                    {
                        if (gRow["Description"].ToString() != "")
                        {
                            retValue.Append(tab).Append("/// " + gRow["Description"].ToString() + "\n");
                        }
                    }
                    catch (System.Data.StrongTypingException)
                    {
                        retValue.Append(tab).Append("/// [No Description] \n");
                    }
                    retValue.Append(tab).Append("/// </summary>\n");

                    #endregion

                    retValue.Append(tab).Append("namespace " + gRow["ResourcesGroupName"].ToString() + "\n");
                    retValue.Append(tab).Append("{\n");

                    string GropuId = gRow["ResourceGroupId"].ToString();


                    #region 生成权限信息

                    DataView resDv = new DataView(resDt);
                    if (GropuId != string.Empty)
                    {
                        resDv.RowFilter = "[ResourceGroupId] ='" + GropuId + "'";
                    }

                    //生成权限结构
                    foreach (DataRowView row in resDv)
                    {
                        retValue.Append(tab).Append("\t/// <summary>\n");
                        retValue.Append(tab).Append("\t/// " + row["ResourcesCNName"].ToString() + "\n");
                        try
                        {
                            if (row["Description"].ToString() != "")
                                retValue.Append(tab).Append("\t/// [" + row["Description"].ToString() + "]\n");
                        }
                        catch (System.Data.StrongTypingException)
                        {
                            retValue.Append(tab).Append("\t/// [No Description] \n");
                        }
                        retValue.Append(tab).Append("\t/// </summary>\n");
                        retValue.Append(tab).Append("\t/// <value>" + row["ResourcesId"].ToString() + "</value>\n");


                        //retValue.Append(tab).Append("\tpublic const string " + row["ResourcesName"].ToString() + " = \"" + row["ResourcesId"].ToString() + "\";\n\n");
                        #region 生成Privilege


                        retValue.Append(tab).Append("\tpublic struct " + row["ResourcesName"].ToString() + "\n");
                        retValue.Append(tab).Append("\t{\n");
                        foreach (MemberShipDAL.DataObject.DataResources.wmf_PrivilegeRow privilegeRow in privilegeDt.Select("[ResourcesId]='" + row["ResourcesId"].ToString() + "'"))
                        {
                            MemberShipDAL.DataObject.DataResources.wmf_OperationRow[] optRows = (MemberShipDAL.DataObject.DataResources.wmf_OperationRow[])optDt.Select("[OperationId]=" + privilegeRow.OperationId);
                            retValue.Append(tab).Append("\t\t/// <summary>\n");
                            retValue.Append(tab).Append("\t\t/// 资源：" + row["ResourcesCNName"].ToString() + "\n");
                            retValue.Append(tab).Append("\t\t/// 操作：" + optRows[0].OperationCNName + "\n");
                            retValue.Append(tab).Append("\t\t/// </summary>\n");
                            retValue.Append(tab).Append("\t\t public const string " + optRows[0].OperationName + " = \"" + privilegeRow.PrivilegeId + "\";\n\n");
                        }

                        retValue.Append(tab).Append("\t}\n");
                        #endregion

                    }
                    #endregion
                    retValue.Append(GetPrivilegeString(GropuId, retValue, resDt, resGrpDt, privilegeDt, optDt, tab));


                    retValue.Append(tab).Append("}\n");
                }
                else
                {
                    #region 注释
                    genCode.Append(tab).Append("/// <summary>\n");
                    try
                    {
                        if (gRow["Description"].ToString() != "")
                        {
                            genCode.Append(tab).Append("/// " + gRow["Description"].ToString() + "\n");
                        }
                    }
                    catch (System.Data.StrongTypingException)
                    {
                        genCode.Append(tab).Append("/// [No Description] \n");
                    }
                    genCode.Append(tab).Append("/// </summary>\n");

                    #endregion

                    genCode.Append(tab).Append("namespace " + gRow["ResourcesGroupName"].ToString() + "\n");
                    genCode.Append(tab).Append("{\n");

                    string GropuId = gRow["ResourceGroupId"].ToString();


                    #region 生成权限信息

                    DataView resDv = new DataView(resDt);
                    if (GropuId != string.Empty)
                    {
                        resDv.RowFilter = "[ResourceGroupId] ='" + GropuId + "'";
                    }

                    //生成权限结构
                    foreach (DataRowView row in resDv)
                    {
                        genCode.Append(tab).Append("\t/// <summary>\n");
                        genCode.Append(tab).Append("\t/// " + row["ResourcesCNName"].ToString() + "\n");
                        try
                        {
                            if (row["Description"].ToString() != "")
                                genCode.Append(tab).Append("\t/// [" + row["Description"].ToString() + "]\n");

                        }
                        catch (System.Data.StrongTypingException)
                        {
                            genCode.Append(tab).Append("\t/// [No Description] \n");
                        }
                        genCode.Append(tab).Append("\t/// </summary>\n");
                        genCode.Append(tab).Append("\t/// <value>" + row["ResourcesId"].ToString() + "</value>\n");


                        //genCode.Append(tab).Append("\tpublic const string " + row["ResourcesName"].ToString() + " = \"" + row["ResourcesId"].ToString() + "\";\n\n");
                        #region 生成Privilege


                        genCode.Append(tab).Append("\tpublic struct " + row["ResourcesName"].ToString() + "\n");
                        genCode.Append(tab).Append("\t{\n");
                        foreach (MemberShipDAL.DataObject.DataResources.wmf_PrivilegeRow privilegeRow in privilegeDt.Select("[ResourcesId]='" + row["ResourcesId"].ToString() + "'"))
                        {
                            MemberShipDAL.DataObject.DataResources.wmf_OperationRow[] optRows = (MemberShipDAL.DataObject.DataResources.wmf_OperationRow[])optDt.Select("[OperationId]=" + privilegeRow.OperationId);
                            genCode.Append(tab).Append("\t\t/// <summary>\n");
                            genCode.Append(tab).Append("\t\t/// 资源：" + row["ResourcesCNName"].ToString() + "\n");
                            genCode.Append(tab).Append("\t\t/// 操作：" + optRows[0].OperationCNName + "\n");
                            genCode.Append(tab).Append("\t\t/// </summary>\n");
                            genCode.Append(tab).Append("\t\t public const string " + optRows[0].OperationName + " = \"" + privilegeRow.PrivilegeId + "\";\n\n");
                        }

                        genCode.Append(tab).Append("\t}\n");
                        #endregion


                    }
                    #endregion
                    genCode.Append(GetPrivilegeString(GropuId, genCode, resDt, resGrpDt, privilegeDt, optDt, tab));


                    genCode.Append(tab).Append("}\n");
                }


            }
            return retValue.ToString();
        }
    }
}
